package com.sysu.dinner.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sysu.dinner.model.User;

@Component("userDao")
public class UserDAO {
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	public boolean isExist(String name,String phone)
	{
		List<User> users = new ArrayList<User>();
		users = hibernateTemplate.find("from User user where name='"+name+"' and phoneNumber='"+phone+"'");
		if(users.size()>0)
			return true;
		return false;
	}
	
	public int addUser(User user)
	{
		if(hibernateTemplate.save(user) != null)
			return 1;
		return -1;
	}
	
	@SuppressWarnings("unchecked")
	public boolean isValid(String name,String password,String phone){
		List<User> users = new ArrayList<User>();
		users = hibernateTemplate.find("from User user where name='"+name+"' and password='"+password+"' and phoneNumber='"+phone+"'");
		if(users.size()>0)
			return true;
		return false;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	
	

}
